package com.scalar.chiptrack.finance.creditNote.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.creditNote.forms.CreditNoteForm;
import com.scalar.chiptrack.finance.creditNote.dao.CreditNoteDAO;
import com.scalar.chiptrack.sales.dao.SalesOrderDAO;
import com.scalar.chiptrack.sales.SalesOrder;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.operations.packingslip.dao.PackingSlipDAO;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;

/**
 * User: Sujan
 * Date: Apr 16, 2007
 * Time: 7:22:26 PM
 */
public class CreditNoteNewCommand extends ActionCommand
{

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HttpSession session  = request.getSession();
        User user            = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
        CreditNoteForm creditNoteForm = (CreditNoteForm) form;

        creditNoteForm.setCreditNoteNumber( CreditNoteDAO.generateAutomatedCreditNoteNo( creditNoteForm.getNoteType() ) );

        PackingSlipForm packingSlipForm = NewPackingSlipDAO.loadPackingSlipById( creditNoteForm.getCustomerShippingId() );
        creditNoteForm.setSalesOrderId( packingSlipForm.getSalesOrderId() );
        creditNoteForm.setBillingAddress( packingSlipForm.getBillingAddressObject() );
        creditNoteForm.setShippingAddress( packingSlipForm.getShippingAddressObject() );
        creditNoteForm.setPackingSlipLabel( packingSlipForm.getLabel() );

        SalesOrder salesOrder = SalesOrderDAO.loadSalesOrder( creditNoteForm.getSalesOrderId()+"" );

        creditNoteForm.setIssueDate( StringUtils.formatDateToString( new Date() ) );
        creditNoteForm.setCustomerName( salesOrder.getCustomerName() );
        creditNoteForm.setSalesOrderNo( salesOrder.getSalesOrderNo() );
        creditNoteForm.setCustomerPONo( salesOrder.getCustomerPO() );
        creditNoteForm.setSalesRep( salesOrder.getSalesRep() );
        creditNoteForm.setEndCustomerName( salesOrder.getOdm());
        creditNoteForm.setEndUserName( salesOrder.getEndCustomer() );
        creditNoteForm.setPayTerms( salesOrder.getShippingTerms());

        ArrayList shippedLineItems  = CreditNoteDAO.loadLineItemsforNewCreditNote(null, creditNoteForm.getCustomerShippingId());
        creditNoteForm.setLineItems( shippedLineItems );

        return mapping.findForward ( "creditNote" );
    }
}
